package com.opengw.configuration.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

@Entity
@Table(name = "config_group")
public class ConfigGroup implements Serializable{

	private static final long serialVersionUID = 3989717006761620022L;
	private long configGroupId;
	private String groupName;
	private List<ConfigUser> userList;

	public ConfigGroup() {
		this.userList = new ArrayList<ConfigUser>();
	}

	public boolean controlUser(String username, String password) {
		for (ConfigUser configUser : userList) {
			if (username.equals(configUser.getUsername())
					&& password.equals(configUser.getPassword()))
				return true;
		}
		return false;
	}

	public void setUserList(List<ConfigUser> userList) {
		this.userList = userList;
	}

	@ManyToMany
	@JoinTable(name = "config_user_group",
			joinColumns = @JoinColumn(name = "config_group_id"),
			inverseJoinColumns = @JoinColumn(name = "config_user_id"))
	public List<ConfigUser> getUserList() {
		return userList;
	}

	public void setConfigGroupId(long configGroupId) {
		this.configGroupId = configGroupId;
	}
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "group_id")
	public long getConfigGroupId() {
		return configGroupId;
	}

	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	@Column(name = "group_name", unique = true)
	public String getGroupName() {
		return groupName;
	}


}
